######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate the diagnostics system
# -----------------------------------------------------------------
function New-Diagnostics()
{
    $obj=new-object PSObject -Property @{
        AllowLegacyTraceProviders = $false;
		CustomerExperienceImprovementProgramEnabled = $true;
		DaysToKeepLogs = 14;
		DownloadErrorReportingUpdatesEnabled = $true;
		ErrorReportingAutomaticUploadEnabled = $true;
		ErrorReportingEnabled = $true;
		EventLogFloodProtectionEnabled = $true;
		EventLogFloodProtectionNotifyInterval = 5;
		EventLogFloodProtectionQuietPeriod = 2;
		EventLogFloodProtectionThreshold = 5;
		EventLogFloodProtectionTriggerPeriod = 2;
		LogCutInterval = 30;
		LogDiskSpaceUsageGB = 1000;
		LogLocation = $env:CommonProgramFiles + "\Microsoft Shared\Web Server Extensions\14\Logs.";
		LogMaxDiskSpaceUsageEnabled = $false;
		ScriptErrorReportingDelay = 30;
		ScriptErrorReportingEnabled = $true;
		ScriptErrorReportingRequireAuth = $true;
    }
	
	# -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {   
		Validate-NotEmpty "LogLocation" $this.LogLocation
		Validate-Range "DaysToKeepLogs" $this.DaysToKeepLogs 1 366
		Validate-Range "EventLogFloodProtectionNotifyInterval" $this.EventLogFloodProtectionNotifyInterval 1 1440
		Validate-Range "EventLogFloodProtectionQuietPeriod" $this.EventLogFloodProtectionQuietPeriod 1 1440
		Validate-Range "EventLogFloodProtectionThreshold" $this.EventLogFloodProtectionThreshold 1 100
		Validate-Range "EventLogFloodProtectionTriggerPeriod" $this.EventLogFloodProtectionTriggerPeriod 1 1440
		Validate-Range "LogCutInterval" $this.LogCutInterval 1 1440
		Validate-Range "LogDiskSpaceUsageGB" $this.LogDiskSpaceUsageGB 1 1000
		Validate-Range "ScriptErrorReportingDelay" $this.ScriptErrorReportingDelay 0 1440
	}
	
    # -----------------------------------------------------------------
    # Prepares the server for the diagnostics configuration
    # -----------------------------------------------------------------
    Add-Method $obj "PrepareServers" `
    {
        New-SPMServerDirectory -Path $this.LogLocation -Servers $farm.Servers
    }
    # -----------------------------------------------------------------
    # Configures the diagnostics system
    # -----------------------------------------------------------------
    Add-Method $obj "Configure" `
    {
		Write-Operation "Configuring diagnostic settings"
		$diagnosticsConfig = Get-SPDiagnosticConfig
		
		Set-SPDiagnosticConfig -InputObject $diagnosticsConfig `
			-AllowLegacyTraceProviders:$this.AllowLegacyTraceProviders `
			-CustomerExperienceImprovementProgramEnabled:$this.CustomerExperienceImprovementProgramEnabled `
			-DaysToKeepLogs $this.DaysToKeepLogs `
			-DownloadErrorReportingUpdatesEnabled:$this.DownloadErrorReportingUpdatesEnabled `
			-ErrorReportingAutomaticUploadEnabled:$this.ErrorReportingAutomaticUploadEnabled `
			-ErrorReportingEnabled:$this.ErrorReportingEnabled `
			-EventLogFloodProtectionEnabled:$this.EventLogFloodProtectionEnabled `
			-EventLogFloodProtectionNotifyInterval $this.EventLogFloodProtectionNotifyInterval `
			-EventLogFloodProtectionQuietPeriod $this.EventLogFloodProtectionQuietPeriod `
			-EventLogFloodProtectionThreshold $this.EventLogFloodProtectionThreshold `
			-EventLogFloodProtectionTriggerPeriod $this.EventLogFloodProtectionTriggerPeriod `
			-LogCutInterval $this.LogCutInterval `
			-LogDiskSpaceUsageGB $this.LogDiskSpaceUsageGB `
			-LogLocation $this.LogLocation `
			-LogMaxDiskSpaceUsageEnabled:$this.LogMaxDiskSpaceUsageEnabled `
			-ScriptErrorReportingDelay $this.ScriptErrorReportingDelay `
			-ScriptErrorReportingEnabled:$this.ScriptErrorReportingEnabled `
			-ScriptErrorReportingRequireAuth:$this.ScriptErrorReportingRequireAuth
     }
     return $obj
}
